
$(function() {

	$("a[id^='mold_']").click(function() {
		return false;
	});

});

var Produce = {
	ss: 22,

	popup: function (e, p2, id, status) {
		$.cookie('id_', id);

		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'mold.php?do=Produce.edit&id=' + id + p2;
		var c2 = 'mold.php?do=Produce.view&id=' + id + p2;

		var c3 = 'mold.php?do=Produce.status&id=' + id + p2;
		var c4 = 'mold.php?do=Produce.reason&id=' + id + p2;

		var c5 = 'mold.php?do=Quality.add&id=' + id;

		var a1 = ['编辑信息', 'url', 'Produce.dialog_edit_', '600x400', '编辑信息'];
		var a2 = ['查看信息', 'url', 'Produce.dialog_view_', '600x400', '查看信息'];

		var a3 = ['开始质检', 'url', 'Produce.list_start'];

		var a4 = ['暂停质检', 'url', 'Produce.list_stop'];
		var a5 = ['取消质检', 'url', 'Produce.list_cancel'];

		var a6 = ['继续质检', 'url', 'Produce.list_continue'];

		var a7 = ['质检合格', 'url', 'Produce.dialog_quality_', '600x400', '添加质检报告'];
		var a8 = ['质检异常', 'url', 'Produce.dialog_quality_', '600x400', '添加质检报告'];
		
		var a9 = ['重新质检', 'url', 'Produce.list_restart'];

		if (status == 1) {
			var s = c3 + "&status=2, " + c1 + ", " + c2 + ", " + c3 + "&status=4, " + c3 + "&status=5";

			return popup(e, [a3, a1, a2, a4, a5], s);
		}

		if (status == 2 || status == 3) {
			var s = c5 + "&status=9, " + c5 + "&status=1, " + c2 + ", " + c3 +"&status=4, " + c3 + "&status=5";

			return popup(e, [a7, a8, a2, a4, a5], s);
		}

		if (status == 4 || status == 5) {
			var s = c3 + "&status=2, " + c2;

			return popup(e, [a6, a2], s);
		}

		if (status == 8) {
			var s = c3 + "&status=3, " + c2;

			return popup(e, [a9, a2], s);
		}

		var s = c2;

		return popup(e, [a2], s);
	},

	dialog_edit_: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('mold.php?do=Produce.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},
					
					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						body.content = form.content.value;
						body.t1 = form.t1.value;
						body.t2 = form.t2.value;
						body.hour = form.hour.value;
						body.minute = form.minute.value;

						$(this).dialog("close");
						
						var url = 'mold.php?do=Produce.editPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});	
	},

	dialog_view_: function (url, title, xy) {
		$('#dialog_view').dialog('option', 'buttons', {

			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"打印": function() {
				var id = $.cookie('id_');

				var url = 'mold.php?do=Produce.view&tpl=doc&id=' + id;

				jswin(url, 'plan_print', '800x600');
			}
		});

		dialog_view(url, title, xy)
	},

	list_start: function (url, view) {
		$.getJSON('mold.php?do=Produce.ajax_b', function(json) {
			if (json.status == 1) {
				if (confirm("是否要开始质检？")) {
					list(url, view);
				}
			} else {
				_error();
			}
		});	
	},

	list_stop: function (url, view) {
		$.getJSON('mold.php?do=Produce.ajax_b', function(json) {
			if (json.status == 1) {
				if (confirm("是否暂停质检？")) {
					list(url, view);
				}
			} else {
				_error();
			}
		});	
	},

	list_cancel: function (url, view) {
		$.getJSON('mold.php?do=Produce.ajax_b', function(json) {
			if (json.status == 1) {
				if (confirm("是否取消质检？")) {
					list(url, view);
				}
			} else {
				_error();
			}
		});	
	},

	list_continue: function (url, view) {
		$.getJSON('mold.php?do=Produce.ajax_b', function(json) {
			if (json.status == 1) {
				if (confirm("是否继续质检？")) {
					list(url, view);
				}
			} else {
				_error();
			}
		});	
	},

	list_restart: function (url, view) {
		$.getJSON('mold.php?do=Produce.ajax_b', function(json) {
			if (json.status == 1) {
				if (confirm("是否重新质检？")) {
					list(url, view);
				}
			} else {
				_error();
			}
		});	
	},

	dialog_quality_: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('mold.php?do=Produce.ajax_b', function(json) {
			if (json.status == 1) {
				$('#dialog_add').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},
					
					"添加": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_add")[0];

						var body = { };
						body.id = form.id.value;
						
						body.content = form.content.value;
						body.exception = form.exception.value;

						body.date = form.date.value;
						body.status = form.status.value;

						var url = 'mold.php?do=Quality.addPost';

						$.post(url, body, function(data) {
							var a = $.evalJSON(data);

							var s = "mold.php?do=Quality.ajax_upload&id=" + a.id + "&name=" + $("#filename").val();
							$('#userfile').uploadifySettings('script', s);

							if ($("#_upload").val() == "1") {
								$("#dialog_add").dialog("close");
							} else {
								$('#userfile').uploadifyUpload();
							}

							if ($("#barcode").val() == "1") {
								alert("操作成功！");
								$('#result').html('');
							} else {
								Produce.list_();
							}
						});
					}
				});

				dialog_add(url, title, xy);
			} else {
				_error();
			}
		});
	},

	list_: function () {
		var result = Common.result();
		var url = 'mold.php?do=Produce.findPost';

		var p2 = $.cookie('p2');
		if (p2 != null) {
			url += p2;
		}

		$.get(url, function(data) {
			$(result).html(data);
		});
	}
};